home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Documents / JAVA Programming / examples / 12 / StackDemo.java < prev    next >
Encoding:
Java Source  |  2000-09-08  |  726 b   |  32 lines

  1. import java.util.Stack;
  2. import java.util.EmptyStackException;                        
  3. class StackDemo {
  4. static void showpush(Stack st, int a) { 
  5. st.push(new Integer(a));
  6. System.out.println("push(" + a + ")");
  7. System.out.println("stack: " + st);
  8. }
  9. static void showpop(Stack st) { 
  10. System.out.print("pop -> ");
  11. Integer a = (Integer) st.pop();
  12. System.out.println(a);
  13. System.out.println("stack: " + st);
  14. }
  15.  
  16. public static void main(String args[]) { 
  17. Stack st = new Stack();
  18. System.out.println("stack: " + st);
  19. showpush(st, 42);
  20. showpush(st, 66);
  21. showpush(st, 99);
  22. showpop(st);
  23. showpop(st);
  24. showpop(st);
  25. try {
  26. showpop(st);
  27. catch (EmptyStackException e) { 
  28. System.out.println("empty stack");
  29. } }
  30.